#include "addfrienddialog.h"
#include <qscrollbar.h>
#include"model/datacenter.h"

AddFriendDialog::AddFriendDialog(QWidget *parent)
    :QDialog(parent)
{
    this->setAttribute(Qt::WA_DeleteOnClose);

    // 1. 设置基本属性
    this->setWindowTitle("添加好友");
    this->setWindowIcon(QIcon(":/image/logo.png"));
    this->setFixedSize(500, 500);
    this->setStyleSheet("QDialog {background-color: rgb(255, 255, 255);}");

    // 2. 设置核⼼布局
    QGridLayout* layout = new QGridLayout();
    layout->setContentsMargins(30, 30, 30, 30);
    layout->setSpacing(10);
    this->setLayout(layout);

    // 3. 创建搜索框
    searchEdit = new QLineEdit();
    searchEdit->setFixedHeight(50);
    searchEdit->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
    searchEdit->setPlaceholderText("按⼿机号/⽤⼾序号查找");
    searchEdit->setStyleSheet("QLineEdit { border-radius: 10px; backgroundcolor: rgb(243, 243, 243); border: none; padding-left: 10px; font-size: 16px;}");

    // 此处要使⽤ textEdit 信号, 在 setText 的时候不会触发.
    // 不要使⽤ textChanged!
    // connect(searchEdit, &QLineEdit::textEdited, this, [=]() {
    //     // 获取到输⼊框内容并清空
    //     const QString& text = searchEdit->text();
    //     searchEdit->setText("");
    //     this->setSearchKey(text);
    // });

    // 搜索框横跨 8 列
    layout->addWidget(searchEdit, 0, 0, 1, 8);

    // 4. 创建搜索按钮
    QPushButton* searchBtn = new QPushButton();
    searchBtn->setFixedSize(50, 50);
    searchBtn->setIcon(QIcon(":/images/search.png"));
    searchBtn->setIconSize(QSize(30, 30));

    QString style = "QPushButton { background-color: rgb(243, 243, 243);border: none; border-radius: 10px;}";
    style += "QPushButton:hover {background-color: rgb(219, 219, 219);}QPushButton:pressed {background-color: rgb(206, 206, 206);}";

    searchBtn->setStyleSheet(style);
    connect(searchBtn, &QPushButton::clicked, this,&AddFriendDialog::clickSearchBtn);

    // 搜索按钮占 1 ⾏
    layout->addWidget(searchBtn, 0, 8, 1, 1);

    // 5. 创建结果展⽰区
    initResultArea(layout);
}

// 给结果中添加⼀个元素
void AddFriendDialog::addResult(const model::UserInfo &userInfo)
{
    FriendResultItem* item = new FriendResultItem(userInfo);
    resultContainer->layout()->addWidget(item);
}

void AddFriendDialog::clear()
{
    QVBoxLayout* layout = dynamic_cast<QVBoxLayout*>(resultContainer->layout());
    for (int i = layout->count() - 1; i >= 0; --i) {
        QWidget* w = layout->itemAt(i)->widget();
        if (w == nullptr) {
            continue;
        }
        layout->removeWidget(w);
        delete w;
    }
}

void AddFriendDialog::setSearchKey(const QString &searchKey)
{
    searchEdit->setText(searchKey);
}

void AddFriendDialog::clickSearchBtnDone()
{
    model::DataCenter* dataCenter = model::DataCenter::getInstance();
    // 1. 获取到搜索结果数据
    QList<model::UserInfo>* searchResult = dataCenter->getSearchUserResult();
    if (searchResult == nullptr) {
        LOG() << "获取好友搜索结果失败!";
        return;
    }
    // 2. 清空原有数据
    this->clear();
    // 3. 构造新数据
    for (const auto& userInfo : *searchResult) {
        this->addResult(userInfo);
    }
}

void AddFriendDialog::initResultArea(QGridLayout *layout)
{
    // 1. 创建滚动区域
    QScrollArea* scrollArea = new QScrollArea();
    scrollArea->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    // ⼀定要添加这个设置, 否则⽆法正确显⽰.
    scrollArea->setWidgetResizable(true);

    // 隐藏⽔平滚动条. 把垂直滚动条设置的细⼀些.
    scrollArea->verticalScrollBar()->setStyleSheet("QScrollBar:vertical {width: 2px; background-color: rgb(255, 255, 255); }\
    QScrollBar::handle:vertical {background-color: rgb(205, 205, 205);}");
    scrollArea->horizontalScrollBar()->setStyleSheet("QScrollBar:horizontal {height: 0px;}");
    scrollArea->setStyleSheet("QScrollArea { border: none; }");

    // 2. 创建滚动区核⼼ Widget
    resultContainer = new QWidget();
    resultContainer->setObjectName("resultContainer");
    resultContainer->setStyleSheet("#resultContainer {background-color:rgb(255, 255, 255);}");
    scrollArea->setWidget(resultContainer);

    // 3. 给核⼼ Widget 添加布局管理器
    QVBoxLayout* vlayout = new QVBoxLayout();
    vlayout->setContentsMargins(0, 0, 0, 0);
    vlayout->setSpacing(10);
    vlayout->setAlignment(Qt::AlignTop);
    resultContainer->setLayout(vlayout);

    // 4. 添加到布局管理器中, 占据 9 列空间.
    layout->addWidget(scrollArea, 1, 0, 1, 9);

// 5. 添加结果元素
#if TEST_UI
    for (int i = 0; i < 20; i++) {
        model::UserInfo* userInfo = new model::UserInfo();
        userInfo->avatar = QIcon(":/images/default_avatar.jpg");
        userInfo->nickname = "张三";
        userInfo->description = "我的个性签名";
        this->addResult(*userInfo);
    }
#endif
}

void AddFriendDialog::clickSearchBtn()
{
    // 1. 获取到输⼊框的内容
    const QString& searchKey = searchEdit->text();
    if (searchKey.isEmpty()) {
        return;
    }
    model::DataCenter* dataCenter = model::DataCenter::getInstance();
    // 2. 创建处理响应的回调
    connect(dataCenter, &model::DataCenter::searchUserDone, this, &AddFriendDialog::clickSearchBtnDone, Qt::UniqueConnection);
    // 3. 发送请求
    dataCenter->searchUserAsync(searchKey);
}

FriendResultItem::FriendResultItem(const model::UserInfo &userInfo)
    :userInfo(userInfo)
{
    // 1. 设置基本属性
    this->setFixedHeight(70);
    this->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);

    // 2. 创建核⼼布局
    QGridLayout* layout = new QGridLayout();
    layout->setContentsMargins(0, 0, 30, 0);
    layout->setSpacing(10);
    this->setLayout(layout);

    // 3. 创建头像
    QPushButton* avatarBtn = new QPushButton();
    avatarBtn->setFixedSize(50, 50);
    avatarBtn->setIconSize(QSize(50, 50));
    avatarBtn->setIcon(userInfo.avatar);

    // 4. 创建名字
    QLabel* nameLabel = new QLabel();
    nameLabel->setFixedHeight(35);
    nameLabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
    nameLabel->setAlignment(Qt::AlignLeft | Qt::AlignVCenter);
    nameLabel->setStyleSheet("QLabel { font-size: 16px; font-weight: 600;}");
    nameLabel->setText(userInfo.nickname);

    // 5. 创建签名
    QLabel* signatureLabel = new QLabel();
    signatureLabel->setFixedHeight(35);
    signatureLabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
    signatureLabel->setAlignment(Qt::AlignLeft | Qt::AlignVCenter);
    signatureLabel->setText(userInfo.description);

    // 6. 创建添加按钮
    addBtn = new QPushButton();
    addBtn->setFixedSize(100, 40);
    addBtn->setText("添加好友");
    addBtn->setStyleSheet("QPushButton { border: none; background-color:rgb(137, 217, 97); color: rgb(255, 255, 255); border-radius: 10px; }\
    QPushButton:pressed { background-color: rgb(198, 198, 198); }");
    //connect(addBtn, &QPushButton::clicked, this, &FriendResultItem::clickAddBtn);

    // 7. 添加到布局管理器中
    layout->addWidget(avatarBtn, 0, 0, 2, 1);
    layout->addWidget(nameLabel, 0, 1, 1, 1);
    layout->addWidget(signatureLabel, 1, 1, 1, 1);
    layout->addWidget(addBtn, 0, 2, 2, 1);

    connect(addBtn, &QPushButton::clicked, this, &FriendResultItem::clickAddBtn);
}

void FriendResultItem::clickAddBtn()
{
    model::DataCenter* dataCenter = model::DataCenter::getInstance();
    // 1. 发送好友申请
    dataCenter->addFriendApplyAsync(userInfo.userId);
    // 2. 发送按钮设置为 "禁⽤状态"
    addBtn->setEnabled(false);
    addBtn->setText("已申请");
    addBtn->setStyleSheet("QPushButton { color: rgb(255, 255, 255); backgroundcolor: rgb(198, 198, 198); border-radius: 10px; }");
}
